package com.bike.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import com.mysql.fabric.xmlrpc.base.Array;


public class IpUtil {
	public static String  getCityByIp(String ip){
		String city="";
		 InputStream is=null;
		try {
			String spec = "http://whois.pconline.com.cn/ip.jsp?ip="+ip;  
	        URL url = new URL(spec);  
	        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
	        conn.setRequestMethod("GET");  
	        conn.setDoInput(true);  
	        conn.setDoOutput(true);  
	        conn.setInstanceFollowRedirects(true);  
	        conn.connect();// 握手  
	        is= conn.getInputStream();//拿到输入流
	        byte[] bytes=new byte[50];
	        is.read(bytes);
	        city=new String(bytes,"gbk").trim().split(" ")[0];
	    } catch (Exception e) {  
	    	e.printStackTrace();
	         city="未知";
	    }  finally {
	    	if(is!=null)
				try {
					is.close();
				} catch (IOException e) {
				} 
		}
		 return city;
	}

	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	public static String  getparcelWuliu(String domain){
		String city="";
		try {
			String spec = "http://pandavip.www.net.cn/check/checkdomain?callback=jQuery111102605717696808636_1437562098271&domain="+domain+".com&_=1437562098332&isg2=AuXl5KLnetShc204V3rEY-6d1asdg5mz";  
			URL url = new URL(spec);  
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
			conn.setRequestMethod("GET");  
			conn.setDoInput(true);  
			conn.setDoOutput(true);  
			conn.setInstanceFollowRedirects(true);  
			conn.connect();// 握手  
			InputStream is = conn.getInputStream();//拿到输入流  
			byte[] bytes=new byte[4096];
			is.read(bytes);
			city=new String(bytes,"utf-8");
			is.close();  
		} catch (Exception e) {  
			e.printStackTrace();
		}  
		return city;
	}
	
	public static void main(String[] args) throws IOException, InterruptedException {
		//getdomain();
		System.out.println(getCityByIp("58.15.26.120"));
	}
	public static void getdomain() throws IOException, InterruptedException {
		 File file=new  File("D:/domain.txt");
		 FileWriter os=new FileWriter(file,true);
		 BufferedWriter bos=new BufferedWriter(os);
		 String [] one1=new String[]{"b","q","w","r","t","y"};
		String [] one=new String[]{"b","q","w","r","t","y","p","s","d","f","g","h","zh","ch","sh","j","k","l","z","x","c","n","m"};
		String [] two=new String[]{"a","ai","an","ang","eng","ing","ong","en","in","un","vn","ei","ui","ao","ou"
				,"iu","ie","ve","er","e","i","o","u","v"};
		for(int i=0;i<one1.length;i++){
			String domain=one1[i];
			for(int j=3;j<two.length;j++){
				String domain1=domain+two[j];
				for(int t=3;t<one.length;t++){
					 String domain2=domain1+one[t];
					for(int p=0;p<two.length;p++){
						String domain3=domain2+two[p];
						Thread.sleep(500);
						String result=getparcelWuliu(domain3);
						if(result.indexOf(":1")!=-1){
							System.out.println(domain3);
							bos.append(domain3+"-");
							bos.flush();
						} else{
							System.out.println(p);
						}
					}
				}
			}
		}
		bos.flush();
		bos.close();
		os.close();
	}
}
